package com.daxia.ogmall.util;

import org.springframework.transaction.annotation.Transactional;

import com.daxia.ogmall.common.UserType;
import com.daxia.ogmall.dto.SpecificationDTO;
import com.daxia.ogmall.dto.UserDTO;
import com.daxia.ogmall.service.SpecificationService;
import com.daxia.ogmall.service.UserService;
import com.daxia.ogmall.support.SpringContextHolder;

public class PriceUtils {
	
	@Transactional
	public static double getPriceFactor(Long userId, Long specificationId) {

		if (MyWebUtils.getCurrentRequest() == null) {
			return 1.0d;
		}
		
		UserService userService = SpringContextHolder.getBean(UserService.class);
		SpecificationService specificationService = SpringContextHolder.getBean(SpecificationService.class);
		UserDTO coreUser = userService.load(userId);
		
		UserDTO user = userService.load(coreUser.getId());
		// 如果用户本身的不为空就返回本身的
		if (user.getPriceFactor() != null) {
			return user.getPriceFactor();
		}
		// 如果部门不为空
		if (user.getDepartment() != null && user.getDepartment().getPriceFactor() != null) {
			return user.getDepartment().getPriceFactor();
		}
		
		SpecificationDTO specificationDTO = specificationService.load(specificationId);
		Double factor = userService.load(specificationDTO.getProduct().getUser().getId()).getPriceFactor();
		if (factor == null) {
			factor = 1d;
		}
		if (user.getType() == null) { // 未知类型
			return factor * 2;
		}
		if (user.getType() == UserType.City.getValue()) {
			return factor;
		}
		if (user.getType() == UserType.DecorateCompany.getValue()) {
			return factor * 1.06;
		}
		if (user.getType() == UserType.Designer.getValue()) {
			return factor * 1.15;
		}
		if (user.getType() == UserType.Normal.getValue()) {
			return factor * 2;
		}
		if (user.getType() == UserType.Manager.getValue()) {
			return 1.0d; // 管理员看到出厂价
		}
		return factor * 2; // 默认就是2倍	
		
	}

}
